from module.static_module.parent.view import SubWindowView
from tkinter import Menu
import tkinter.font as tkFont
from tools.view import *
from conf import conf
from module.static_module.running.view import RunningView
from module.static_module.operate.view import OperateView
from module.static_module.file_info.view import FileInfoView
from module.static_module.file_action.view import FileActionView


class MainWindowView:
    def __init__(self, master):
        self.master = master
        self.root = tk.Tk()
        self.root.title("DDQuant")
        self.root.geometry(f"{conf.View.main_win_size.value[0]}x{conf.View.main_win_size.value[1]}")

        # 获取默认字体
        default_font = tkFont.nametofont("TkDefaultFont")
        # 更改默认字体的大小和字体族
        default_font.configure(family=conf.View.font.value[0], size=conf.View.font.value[1])

        # 创建菜单栏
        menu_bar = Menu(self.root)
        self.root.config(menu=menu_bar)

        # 设置网格权重
        for i in range(2):
            self.root.grid_rowconfigure(i, weight=1)
        for j in range(3):
            self.root.grid_columnconfigure(j, weight=1)

        # 添加模块菜单
        module_menu = Menu(menu_bar, tearoff=0)
        menu_bar.add_cascade(label="模块", menu=module_menu)
        for module_o in self.master.main_window_model.module_ls:
            module_menu.add_command(label=module_o.value,
                                    command=lambda module=module_o: self.on_change_module_name(module))

        # 添加模型菜单
        self.model_menu = Menu(menu_bar, tearoff=0)
        menu_bar.add_cascade(label="模型", menu=self.model_menu)
        # 获取当前所有的模型，将其呈现至菜单栏，为所有module模块第一项设置当前模型，并将结果赋值给第一个组件
        for model_o in self.master.main_window_model.model_ls:
            self.model_menu.add_command(label=model_o,
                                        command=lambda model=model_o: self.on_change_model_name(model))

        # 添加
        tools_menu = Menu(menu_bar, tearoff=0)
        menu_bar.add_cascade(label="工具", menu=tools_menu)
        tools_menu.add_command(label=Module.GenModel.value, command=lambda: self.gen_model())
        tools_menu.add_command(label=Module.FactorLibraryReport.value, command=lambda: self.factor_library_report())
        tools_menu.add_command(label=Module.DataMerge.value, command=lambda: self.data_merge())
        tools_menu.add_command(label=Module.RevenueCalculate.value, command=lambda: self.revenue_calculate())
        tools_menu.add_command(label=Module.OrderIntegration.value, command=lambda: self.order_integration())
        tools_menu.add_command(label=Module.MessageOrder.value, command=lambda: self.message_order())
        tools_menu.add_command(label=Module.BacktestingVisualization.value, command=lambda: self.backtesting_visualization())
        tools_menu.add_command(label=Module.SectorGuide.value, command=lambda: self.sector_guide())
        tools_menu.add_command(label=Module.ComparisonChart.value, command=lambda: self.comparison_chart())

        # 添加操作菜单
        operate_menu = Menu(menu_bar, tearoff=0)
        menu_bar.add_cascade(label="操作", menu=operate_menu)
        operate_menu.add_command(label="清空记录", command=lambda: f"测试1")
        operate_menu.add_command(label="刷新", command=lambda: f"测试1")

        # 布局 ---------------------------------------------------
        # 主module（动态）
        self.dynamic_module_frame = tk.Frame(self.root, width=conf.View.dynamic_module_size.value[0],
                                             height=conf.View.dynamic_module_size.value[1])
        self.dynamic_module_frame.grid(row=0, column=0, sticky="nsew")
        self.dynamic_module_frame.pack_propagate(False)  # 防止frame缩放以适应其内容
        self.dynamic_module_view: SubWindowView = None

        # 其他固定位置的窗口
        # Running
        self.running_frame = tk.Frame(self.root, width=conf.View.running_size.value[0],
                                      height=conf.View.running_size.value[1])
        self.running_frame.grid(row=0, column=1, sticky="nsew")
        self.running_frame.pack_propagate(False)  # 防止frame缩放以适应其内容
        self.running_view = RunningView(self)

        # Operate
        self.operate_frame = tk.Frame(self.root, width=conf.View.operate_size.value[0],
                                      height=conf.View.operate_size.value[1])
        self.operate_frame.grid(row=1, column=0, sticky="nsew")
        self.operate_frame.pack_propagate(False)  # 防止frame缩放以适应其内容
        self.operate_view = OperateView(self)

        # FileInfo
        self.file_info_frame = tk.Frame(self.root, width=conf.View.file_info_size.value[0],
                                        height=conf.View.file_info_size.value[1])
        self.file_info_frame.grid(row=0, column=2, rowspan=2, sticky="nsew")
        self.file_info_frame.pack_propagate(False)  # 防止frame缩放以适应其内容
        self.file_info_view = FileInfoView(self)

        # FileAction
        self.file_action_frame = tk.Frame(self.root, width=conf.View.file_action_size.value[0],
                                          height=conf.View.file_action_size.value[1])
        self.file_action_frame.grid(row=1, column=1, sticky="nsew")
        self.file_action_frame.pack_propagate(False)  # 防止frame缩放以适应其内容
        self.file_action_view = FileActionView(self)

    def layout_dynamic_module_view(self):
        from core.map_obj import ModuleViewMap
        self.dynamic_module_view = ModuleViewMap[self.master.file_manager.now_dynamic_module](self)
        self.dynamic_module_view.create_layout_init()

    def init_running(self):
        # 获取当前module
        self.running_view.change_layout()

        for widget in self.running_frame.winfo_children():
            if isinstance(widget, tk.Canvas):
                # 对 Canvas 类型的小部件执行操作
                widget.delete("all")
            elif isinstance(widget, tk.Frame):
                for sub_widget in widget.winfo_children():
                    if isinstance(sub_widget, tk.Text):
                        # 对 Text 类型的小部件执行操作
                        sub_widget.delete("1.0", tk.END)

    def on_change_model_name(self, model_name):
        self.master.main_window_model.on_change_model_name(model_name)

    def on_change_module_name(self, module: Module):
        self.master.main_window_model.on_change_module_name(module)

    def gen_model(self):
        self.master.main_window_model.gen_model()
        pass

    def update_menu(self):
        self.model_menu.delete(0, tk.END)
        for model_o in self.master.main_window_model.model_ls:
            self.model_menu.add_command(label=model_o,
                                        command=lambda model=model_o: self.on_change_model_name(model))

    def factor_library_report(self):
        self.master.main_window_model.factor_library_report()
        pass

    def data_merge(self):
        self.master.main_window_model.data_merge()
        pass

    def revenue_calculate(self):
        self.master.main_window_model.revenue_calculate()
        pass

    def order_integration(self):
        self.master.main_window_model.order_integration()
        pass

    def message_order(self):
        self.master.main_window_model.message_order()
        pass

    def backtesting_visualization(self):
        self.master.main_window_model.backtesting_visualization()
        pass

    def sector_guide(self):
        self.master.main_window_model.sector_guide()
        pass

    def comparison_chart(self):
        self.master.main_window_model.comparison_chart()
        pass
